<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择小区" filterable clearable size="mini">
          <el-option v-for="estate in estatesOptions"
                     :key="estate.id"
                     :label="estate.estateName"
                     :value="estate.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="跟进状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择跟进状态" filterable clearable size="mini">
          <el-option label="待处理" value="0"/>
          <el-option label="跟进中" value="1"/>
          <el-option label="处理完成" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="房屋编号" prop="roomNo">
        <el-input v-model="queryParams.roomId" placeholder="请输入房屋编号" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="业主电话" prop="callPhone">
        <el-input v-model="queryParams.callPhone" placeholder="请输入业主电话" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['module:customerCallRecord:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客服姓名" align="center" prop="sysUserName" />
      <el-table-column label="业主电话" align="center" prop="callPhone" />
      <el-table-column label="处理状态" align="center" prop="state">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'danger'">待处理</el-tag>
            <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="''">跟进中</el-tag>
            <el-tag v-if="scope.row.state === 2" style="cursor: pointer" :type="''">处理完成</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="业主姓名" align="center" prop="roomRealName" />"
      <el-table-column label="打电话时间" align="center" prop="callTime" width="180" />
      <el-table-column label="备注信息说明" align="center" prop="remark" />
      <el-table-column label="相关截图" align="center" prop="recordImgList">
        <template slot-scope="scope">
          <div style="display: flex;">
            <el-image v-for="img in scope.row.recordImgList"
                      style="width: 100px; height: 100px"
                      :src="img"
                      :preview-src-list="scope.row.recordImgList">
            </el-image>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:customerCallRecord:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:customerCallRecord:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改客服跟业主电话沟通记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属小区" prop="estateName">
              <el-input v-model="form.estateName" readonly placeholder="请输入所属小区" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客服姓名" prop="sysUserName">
              <el-input v-model="form.sysUserName" readonly placeholder="请输入客服姓名" size="mini"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="房屋" prop="roomNo">
              <el-input v-model="form.roomNo" readonly placeholder="请输入房屋" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业主电话" prop="callPhone">
              <el-input v-model="form.callPhone" placeholder="请输入业主电话" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通话时间" prop="callTime">
              <el-date-picker clearable size="mini" readonly v-model="form.callTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择打电话时间">
              </el-date-picker>
            </el-form-item>

          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="recordImgList">
              <div style="display: flex;">
                <el-image v-for="img in form.recordImgList"
                          style="width: 80px; height: 100px"
                          :src="img"
                          :preview-src-list="form.recordImgList">
                </el-image>
              </div>
              <imageUpload :uploadType="36" v-model="form.uploadImgShowList" @handleUploadSuccess="chooseImg"
                           @handleRemove="removeImg"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理状态" prop="state">
              <el-radio-group v-model="form.state" size="mini">
                <el-radio :value="1" :label="1">跟进中</el-radio>
                <el-radio :value="2" :label="2">处理完成</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注信息说明" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注信息说明" size="mini"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/module/estate/callrecord/record";
import {listAllEstate} from "@/api/module/estate/estate";

export default {
  name: "Record",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      estatesOptions: [],
      // 客服跟业主电话沟通记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateId: undefined,
        roomNo: undefined,
        callPhone: undefined,
        roomRealName: undefined,
        state: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        state: [
          {
            required: true, message: "请选择处理状态", trigger: "change"
          }
        ],
        remark: [
          {
            required: true, message: "请输入备注信息说明", trigger: "change"
          }
        ],
      }
    }
  },
  created() {
    this.getList();
    this.getEstateList();
  },
  methods: {
    /** 查询客服跟业主电话沟通记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getEstateList() {
      listAllEstate().then(response => {
        this.estatesOptions = response.data;
      });
    },
    chooseImg(data) {
      console.log("chooseImg", data);
      console.log("newImg", data.newImg);
      console.log("type", data.type);
      if(this.form.uploadImgList == undefined || this.form.uploadImgList.length == 0) {
        this.form.uploadImgList = []
      }
      if(this.form.uploadImgShowList == undefined || this.form.uploadImgShowList.length == 0) {
        this.form.uploadImgShowList = []
      }
      if(data.type == 'upload') {
        this.form.uploadImgList.push(data.newImg.name);
        this.form.uploadImgShowList.push(data.newImg.url);
      }
    },
    removeImg(data) {
      console.log('removeImg', data);
      this.form.recordImgList = data.imgUrl;
      if(data.type == 'remove'){
        this.form.uploadImgList.splice(data.index, 1);
        this.form.uploadImgShowList.splice(data.index, 1);
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        wxUserId: undefined,
        sysUserId: undefined,
        sysUserName: undefined,
        roomId: undefined,
        roomNo: undefined,
        roomRealName: undefined,
        callPhone: undefined,
        callTime: undefined,
        remark: undefined,
        recordImgList: [],
        uploadImgList:[],
        uploadImgShowList:[],
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客服跟业主电话沟通记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getRecord(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改客服跟业主电话沟通记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客服跟业主电话沟通记录编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delRecord(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客服跟业主电话沟通记录数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportRecord(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
